package com.example.blog.controller;

import com.example.blog.model.UserInfo;
import com.example.blog.service.UserService;
import com.example.blog.util.ConstVariable;
import com.example.blog.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @description: userController
 * @author: chengbaiyi
 * @create: 2022-08-19 19:32
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 注册
     * @param username 登录名
     * @param nickname 昵称
     * @param password 密码
     * @return 是否注册成功
     */
    @RequestMapping("/reg")
    public int reg(String username, String nickname, String password) {
        int result = 0;
        if (StringUtils.hasLength(username) && StringUtils.hasLength(nickname) && StringUtils.hasLength(password)) {
            // 判断登录名唯一性
            UserInfo tmp = userService.sole(username);
            // 数据库中不存在则添加
            if (tmp == null) {
                // 密码加密
                password = PasswordUtil.encrypt(password);
                // 将信息添加到数据库中
                result = userService.reg(username, nickname, password);
            }
        }
        return result;
    }

    /**
     * 登录
     * @param request 请求
     * @param username 登录名
     * @param password 密码
     * @return 登录信息
     */
    @RequestMapping("/login")
    public boolean login(HttpServletRequest request,String username, String password) {
        if (StringUtils.hasLength(username) && StringUtils.hasLength(password)) {
            UserInfo userInfo = userService.sole(username);
            if (userInfo != null) {
                // 判断密码是否正确
                boolean result = PasswordUtil.decrypt(password, userInfo.getPassword());
                if (result) {
                    // 存储session
                    HttpSession session = request.getSession(true);
                    session.setAttribute(ConstVariable.USER_SESSION_KEY, userInfo);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取当前登录用户信息
     * @param request 请求
     * @return 当前登录用户
     */
    @RequestMapping("/loginuserinfo")
    public UserInfo getLoginUserInfo(HttpServletRequest request) {
        // 从session中获取当前登录用户信息
        UserInfo userInfo = (UserInfo) request.getSession(false).getAttribute(ConstVariable.USER_SESSION_KEY);
        // 去除密码
        userInfo.setPassword("");
        return userInfo;
    }

    /**
     * 退出登录
     * @param request 请求
     * @return 返回 1 退出成功
     */
    @RequestMapping("/logout")
    public int logout(HttpServletRequest request) {
        int result = 0;
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(ConstVariable.USER_SESSION_KEY) != null) {
            session.setAttribute(ConstVariable.USER_SESSION_KEY, null);
            result = 1;
        }
        return result;
    }

    /**
     * 根据 uid 查询作者昵称
     * @param uid uid
     * @return 作者昵称
     */
    @RequestMapping("/getauthor")
    public UserInfo getAuthor(Integer uid) {
        UserInfo data = new UserInfo();
        if (uid > 0) {
            data = userService.getAuthor(uid);
        }
        return data;
    }
}
